package com.ynu.yx.chatroleai.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ynu.yx.chatroleai.converter.CharacterCategoryConverter;
import com.ynu.yx.chatroleai.dao.CharacterCategoryDao;
import com.ynu.yx.chatroleai.dto.CharacterCategoryCreateDto;
import com.ynu.yx.chatroleai.dto.CharacterCategoryUpdateDto;
import com.ynu.yx.chatroleai.dto.CharacterCategoryViewDto;
import com.ynu.yx.chatroleai.model.AICharacter;
import com.ynu.yx.chatroleai.model.CharacterCategory;
import com.ynu.yx.chatroleai.utils.GetNullPropertyNames;
import com.ynu.yx.chatroleai.utils.IdGenerator;
import com.ynu.yx.chatroleai.utils.MsgOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Queue;

@Service
@Slf4j
public class CharacterCategoryService {
    @Resource
    private CharacterCategoryDao characterCategoryDao;
    @Resource
    private GetNullPropertyNames getNullPropertyNames;
    @Resource
    private IdGenerator idGenerator;
    @Resource
    private CharacterCategoryConverter characterCategoryConverter;

    // ------------------------------- 增 ---------------------------------
    // 增加角色分类
    public MsgOperation addCharacterCategory(CharacterCategoryCreateDto CCCDto) {
        if (CCCDto == null) {
            return new MsgOperation("-1", "无有效类别信息");
        }
        if (CCCDto.getCategoryName() == null){
            return new MsgOperation("-2", "类别名称不能为空");
        }
        if (CCCDto.getDescription() == null){
            return new MsgOperation("-3", "类别描述不能为空");
        }
        if (CCCDto.getDescription().length() < 3){
            return new MsgOperation("-3", "类别描述不能少于3个字符");
        }
        CharacterCategory characterCategory = new CharacterCategory();

        String[] nullPropertyNames = getNullPropertyNames.getNullPropertyNames(CCCDto);
        BeanUtils.copyProperties(CCCDto, characterCategory, nullPropertyNames);

        try {
            Long categoryId = idGenerator.generateUniqueId(characterCategoryDao, "categoryId");
            characterCategory.setCategoryId(categoryId);
        } catch (Exception e) {
            log.error("生成分类ID失败", e);
            return new MsgOperation("-4", "生成分类ID失败");
        }

        characterCategory.setIsDeleted(0);
        characterCategoryDao.insert(characterCategory);
        return new MsgOperation("1", "创建分类成功");
    }


    // ------------------------------- 查 ---------------------------------
    public List<CharacterCategoryViewDto> getALLCharacterCategoryList() {
        return characterCategoryConverter.toViewListDto(characterCategoryDao.selectList(null));
    }

    public List<CharacterCategoryViewDto> getCCByCategoryLikeName(String categoryName) {
        QueryWrapper<CharacterCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("category_name", categoryName);
        return characterCategoryConverter.toViewListDto(characterCategoryDao.selectList(queryWrapper));
    }

    public CharacterCategoryViewDto getCCByCategoryId(Long categoryId) {
        QueryWrapper<CharacterCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        return characterCategoryConverter.toViewDto(characterCategoryDao.selectOne(queryWrapper));
    }

    public CharacterCategoryViewDto getCCByCategoryName(String categoryName){
        QueryWrapper<CharacterCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name", categoryName);
        return characterCategoryConverter.toViewDto(characterCategoryDao.selectOne(queryWrapper));
    }

    // ------------------------------- 改 ---------------------------------
    public MsgOperation updateCharacterCategory(CharacterCategoryUpdateDto CCUDto) {
        if (CCUDto == null) {
            return new MsgOperation("-1", "无有效类别信息");
        }
        CharacterCategory characterCategory = new CharacterCategory();
        String[] nullPropertyNames = getNullPropertyNames.getNullPropertyNames(CCUDto);
        BeanUtils.copyProperties(CCUDto, characterCategory, nullPropertyNames);

        UpdateWrapper<CharacterCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("category_id", characterCategory.getCategoryId());
        characterCategoryDao.update(characterCategory, updateWrapper);

        return new MsgOperation("1", "更新分类成功");
    }


    // ------------------------------- 删 ---------------------------------
    public MsgOperation deleteCharacterCategory(Long categoryId) {
        if (categoryId == null) {
            return new MsgOperation("-1", "无有效类别ID");
        }
        UpdateWrapper<CharacterCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("category_id", categoryId);
        updateWrapper.set("is_deleted", 1);
        characterCategoryDao.update(null, updateWrapper);

        return new MsgOperation("1", "删除分类成功");
    }

}
